// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "HYBDAMPParser.h"

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <map>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Parameter/IHYBDAMPService.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (HYBDAMP);
/*
// file://./../../Doc/MarkDown/BulkData/HYBDAMP/HYBDAMP.md>
*/

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
// 以下需要修改
#include <cstdint>

enum HYBDAMPKey : std::uint8_t
{
    HYBDAMPKEY_ID     = 2,
    HYBDAMPKEY_METHOD = 3,
    HYBDAMPKEY_SDAMP  = 4,
    HYBDAMPKEY_KDAMP  = 5,
    HYBDAMPKEY_PRTEIG = 6,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseHYBDAMP (const std::shared_ptr<DBServiceFactory>& dbServiceFactory,
                                                               std::vector<std::string>&                dividedEntryInfo)
{
    // 创建临时节点数据结构，默认值为0
    SG::DataStructure::FEM::HYBDAMPData tempData;

    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId = 1;

    int curIDKey = 1;  // 当前字符串是第几项
    // TODO(bug 001) 默认值条件很多，有待补充
    // 遍历分割后的每个词条
    for (std::string& curStrKey : dividedEntryInfo)
    {
        // 检查当前字符串是否为空白
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            switch (curIDKey)
            {
            case HYBDAMPKEY_KDAMP:
                curStrKey = "NO";
                ;
                break;
            case HYBDAMPKEY_PRTEIG:
                curStrKey = "NO";
                ;
            default:
                curIDKey++;
                continue;
                break;
            }
        }
        // 根据当前词条的位置进行相应的解析
        switch (curIDKey)
        {
        case HYBDAMPKEY_ID:
            tempData.m_id = convertToI64 (curStrKey);
            break;
        case HYBDAMPKEY_METHOD:
            tempData.m_METHOD = convertToI64 (curStrKey);
            break;
        case HYBDAMPKEY_SDAMP:
            tempData.m_SDAMP = convertToI64 (curStrKey);
            break;
        case HYBDAMPKEY_KDAMP:
            std::strncpy (
                tempData.m_KDAMP, boost::algorithm::trim_copy (curStrKey).c_str (), boost::algorithm::trim_copy (curStrKey).size ());
            break;
        case HYBDAMPKEY_PRTEIG:
            std::strncpy (
                tempData.m_PRTEIG, boost::algorithm::trim_copy (curStrKey).c_str (), boost::algorithm::trim_copy (curStrKey).size ());
            break;
        default:
            break;
        }

        // 处理完当前词条后，移动到下一个词条
        curIDKey++;
    }
    // 将解析完成的节点数据添加到数据库服务中
    return dbServiceFactory->get<IHYBDAMPService> ()->Add (tempData);
}